package diplom.uabs.dealing.dealing;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Spinner;


public class BankChoice extends ActionBarActivity {
    public static final String itemID = "ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent rechangeCB = getIntent();
        Boolean rechange = rechangeCB.getBooleanExtra("rechangeCB", false);
        if (!rechange) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            int id = preferences.getInt("CB",-1);
            if(id != -1) {
                Intent intent = new Intent(this, MakeGraph.class);
                intent.putExtra(itemID,id);
                startActivity(intent);
            }
        }
        setContentView(R.layout.activity_bank_choice);
    }

    public void aboutDialog(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BankChoice.this);
        builder.setTitle(R.string.dailogTitleCBAbout)
                .setMessage(R.string.dailogMessageCBAbout)
               // .setIcon(R.drawable.ic_android_cat)
                .setCancelable(false)
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        alert.show();

    }

    public void makeChoice(View v) {
        Spinner centralBank = (Spinner)findViewById(R.id.bankChoice);
        int id = (int)centralBank.getSelectedItemId();
        saveState(id);
        Intent intent = new Intent(this, MakeGraph.class);
        intent.putExtra(itemID,id);
        startActivity(intent);
    }

    private void saveState(int id) {
        CheckBox cb = (CheckBox)findViewById(R.id.rememberCB);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        if (cb.isChecked()) {
            editor.putInt("CB",id);
        } else {
            editor.putInt("CB",-1);
        }
        editor.apply();

    }
}
